package com.wzm.org.service.impl;


import com.wzm.basic.exception.BusinessException;
import com.wzm.basic.service.impl.BaseServiceImpl;
import com.wzm.basic.util.BaiduAiUtils;
import com.wzm.org.domain.Employee;
import com.wzm.org.domain.Shop;
import com.wzm.org.domain.ShopAuditLog;
import com.wzm.org.domain.ShopDto;
import com.wzm.org.mapper.EmployeeMapper;
import com.wzm.org.mapper.ShopAuditLogMapper;
import com.wzm.org.mapper.ShopMapper;
import com.wzm.org.service.ShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        String address = shop.getAddress();
        String name = shop.getName();
        String tel = shop.getTel();
        //一：校验
        //1.校验空值
        if(StringUtils.isEmpty(address) || StringUtils.isEmpty(name) || StringUtils.isEmpty(tel) ){
            throw new BusinessException("数据不能为空");
        }
        //2.校验密码是否一致[思路：在domain中加一个参数就可以接收确认密码]
        //3.校验店铺是否已经入驻过
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if(dbShop != null){//入驻过
            throw new BusinessException("该店铺已入驻，请直接登录。忘记密码请联系管理员!!!");
        }
        //百度AI审核
        if(BaiduAiUtils.textCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法，请重新输入！");
        }
        String imgUrl = "123.207.27.208"+shop.getLogo();
        if(BaiduAiUtils.imgCensor(imgUrl)){
            throw new BusinessException("店铺logo不合法，请重新输入！");
        }
        //二：保存数据
        //1.保存店铺管理员信息
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);

        //2.保存店铺信息
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);

        //将店铺id保存到employee中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改状态：1 - 2
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAudit_id(325L); //写死的，因为现在没有登录，不知道是哪一个管理员审核的
        shopAuditLogMapper.save(shopAuditLog);
        //发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        //发送方
        helper.setFrom("wzm75675@foxmail.com");
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //接收目标方
        helper.setTo(employee.getEmail());
        //邮件主题
        helper.setSubject("店铺激活");
        //邮件内容
        helper.setText("<h2>你的店铺审核通过了<br>" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击此处激活</a></h2>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改状态：1 - 2
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAudit_id(325L); //写死的，因为现在没有登录，不知道是哪一个管理员审核的
        shopAuditLogMapper.save(shopAuditLog);

        //发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        //发送方
        helper.setFrom("wzm75675@foxmail.com");
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //接收目标方
        helper.setTo(employee.getEmail());
        //邮件主题
        helper.setSubject("店铺审核失败");
        //邮件内容
        helper.setText("<h2>你的店铺入驻失败<br>" +
                "<a href='http://localhost:8081/#/register'>点击此处重新入驻</a></h2>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        //删除管理员信息
        employeeMapper.remove(shop.getAdmin_id());
        //删除店铺信息
        shopMapper.remove(shop.getId());
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

    /**
     * 邮件点击激活,设置为正常使用状态
     * @param id
     */
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }


}
